package com.itlong.cloud.controller.system;

import com.itlong.cloud.POJO.DTO.operate.OperateGroupGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUserInfoGetPageDTO;
import com.itlong.cloud.POJO.PO.OperateGroupPO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserInfoGetUserPageVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.operate.IOperateGroupServiceClient;
import com.itlong.cloud.user.operate.IOperateUserInfoServiceClient;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <desc>
 *      运营系统组织管理控制器
 * </desc>
 *
 * @createDate 2017/09/14
 */
@RestController
@RequestMapping("/system/group")
public class OperateGroupController {
    @Autowired
    IOperateGroupServiceClient iOperateGroupServiceClient;

    @Autowired
    IOperateUserInfoServiceClient iOperateUserInfoServiceClient;
    /**
     * <desc>
     *      获取分页列表
     * </desc>
     *
     * @return messageVO
     * @author Jiaqi.X
     * @createDate 2017/09/15
     */
    @RequestMapping(path = "/getPage",method = RequestMethod.POST)
    @ResponseBody
    @AuthAccessRequired
    public Object getGroupPage(OperateGroupGetPageDTO pageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateGroupGetPageDTO.class,request);
            if(StringUtils.isNotBlank(pageDTO.getGroupNum())){
                if(!pageDTO.getGroupNum().matches("[0-9]{1,50}")){
                    pageDTO.setGroupNum(" ");
                }else{
                    pageDTO.setGroupNum(Integer.valueOf(pageDTO.getGroupNum()).toString());
                }
                if(pageDTO.getGroupNum().equals("0")){
                    pageDTO.setGroupNum("");
                }
            }
            Page<OperateGroupPO> page = iOperateGroupServiceClient.getPage(pageDTO);
            page.getRows().forEach(po->{
                po.setGroupNum(StringHandlerUtil.autlSupply(3,po.getGroupNum(),"0"));
            });
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织update】获取分页列表失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      保存组织
     * </desc>
     *
     * @param operateGroupPO 组织实体
     * @return messageVO
     * @author Jiaqi.X
     * @createDate 2017/09/14
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.GROUP_OPERATE_LOG_SAVE)
    @RequestMapping(path = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Object saveGroup(OperateGroupPO operateGroupPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateGroupPO= HttpProtocolUtil.parseRequestParamToDTO(OperateGroupPO.class,request);
            iOperateGroupServiceClient.save(operateGroupPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织update】保存组织失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      更新组织
     * </desc>
     *
     * @param operateGroupPO 组织实体
     * @return messageVO
     * @author Jiaqi.X
     * @createDate 2017/09/14
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.GROUP_OPERATE_LOG_UPDATE)
    @RequestMapping(path = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Object updateGroup(OperateGroupPO operateGroupPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateGroupPO= HttpProtocolUtil.parseRequestParamToDTO(OperateGroupPO.class,request);
            iOperateGroupServiceClient.update(operateGroupPO);
            messageVO=new MessageVO(operateGroupPO.getGroupId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织update】更新组织失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      根据id查询组织
     * </desc>
     *
     * @param operateGroupPO 组织实体
     * @return messageVO
     * @author Jiaqi.X
     * @createDate 2017/09/27
     */
    @RequestMapping(path = "getOne",method = RequestMethod.POST)
    @ResponseBody
    public Object getGroup(OperateGroupPO operateGroupPO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateGroupPO= HttpProtocolUtil.parseRequestParamToDTO(OperateGroupPO.class,request);
            operateGroupPO=iOperateGroupServiceClient.getOne(operateGroupPO);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),operateGroupPO);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织getOne】根据id查询组织失败",e);
        }
        return messageVO;
    }


    /**
     * <desc>
     *      获取所有组织
     * </desc>
     *
     * @return  messageVO
     * @author Jiaqi.X
     * @createDate 2017/09/29
     */
    @RequestMapping(path = "getList",method =  RequestMethod.POST)
    @ResponseBody
    public Object getGroupList(){
        MessageVO messageVO;
        try{
            List<OperateGroupPO> list=iOperateGroupServiceClient.getList();
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织getList】获取所有组织失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      获取分配人员列表
     * </desc>
     *
     * @param pageDTO1  查询参数DTO
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/10
     */
    @RequestMapping(path = "getAllotUserPage",method =  RequestMethod.POST)
    @ResponseBody
    public Object getAllotUserPage(OperateUserInfoGetPageDTO pageDTO1){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO1= HttpProtocolUtil.parseRequestParamToDTO(OperateUserInfoGetPageDTO.class,request);
            OperateUserInfoGetPageDTO pageDTO = pageDTO1;
            Page<OperateUserInfoGetUserPageVO> page=iOperateUserInfoServiceClient.getPage(pageDTO);
            List<OperateUserInfoGetUserPageVO> users =page.getRows();
            //检查是否已拥有该角色
            users.forEach(user->{
                if (user.getGroupId()!=null&&user.getGroupId().equals(pageDTO.getAllotGroupId())) {
                    user.setHaveGroup("true");
                }
            });
            page.setRows(users);
            messageVO=new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),page);
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织getAllotUserPage】获取分配人员列表失败",e);
        }
        return messageVO;
    }


    /**
     * <desc>
     *      部门分配用户
     * </desc>
     *
     * @param groupId    部门id
     * @param allotUsers    需要分配的人员id 多个，号分隔
     * @param userIds 当前列表所有人员id 多个，号分隔
     * @return
     * @author Jiaqi.X
     * @createDate 2017/10/10
     */
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.GROUP_OPERATE_LOG_ALLOT_USER)
    @RequestMapping(path = "saveGroupUsers",method = RequestMethod.POST)
    @ResponseBody
    public Object saveGroupUsers(String groupId,String allotUsers,String userIds){
        MessageVO messageVO;
        try{
            HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            groupId=request.getAttribute("groupId")==null?null:request.getAttribute("groupId").toString();
            allotUsers=request.getAttribute("allotUsers")==null?null:request.getAttribute("allotUsers").toString();
            userIds=request.getAttribute("userIds")==null?null:request.getAttribute("userIds").toString();
            //人员不为空的情况下进行分配操作
            if(StringUtils.isNotBlank(userIds)) {
                iOperateGroupServiceClient.saveGroupUsers(groupId, allotUsers, userIds);
            }
            messageVO=new MessageVO(String.format("%s|%s|%s",groupId,allotUsers,userIds),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_系统_组织saveGroupUsers】部门分配用户失败",e);
        }
        return messageVO;
    }
}
